/*
 * Assignment5.c
 *
 * Created: 4/12/2014 1:27:17 PM
 *  Author: Christian
 */ 

//This program sends reads the temperature from he sensor and sends the 
//data to the input ADC0. We get the temperature from the sensonr and use
//this formula to get the right value to display to the LED 7-seg
//temperature = ((adc_value * 1.1) / 1024) * 100; TempInt = (temperature * 9) / 5 + 32;
//After we get the right value we then send the information to the 7-seg bar
//I used only two digits because one of the pins on the pin was working

//----------------These are the libraries ------------------------------------
#define F_CPU 8000000UL		//Sets the clock to 8MHz
#include <util/delay.h>		//Library for the delays
#include <avr/io.h>			//Library for the inputs and outputs
#include <avr/interrupt.h>	//Library for the interrupts 


//-------- This is the code to display the digits to the 7-seg LED bar -------
int LEDoutput(int n){
	
	int val = 0b00000000;
		//This are the different cases for the Segments of the LED that has to light
		//up. So for example in case 0 its 0b00011111 
		//								   XXGXDCBAFE
		
		if(n == 0){ // If case to display 0
		val= 0b00111111; 
		//   XXGXDCBAFE
		}
		if(n == 1){ // If case to display 1
		val= 0b00011000; 
		//   XXGXDCBAFE
		}
		if(n == 2){ // If case to display 2
		val= 0b10101101; 
		//   XXGXDCBAFE
		}
		if(n == 3){ // If case to display 3
		val= 0b10111100; 
		//	 XXGXDCBAFE
		}
		if(n == 4){ // If case to display 4
		val= 0b10011010; 
		//   XXGXDCBAFE
		}
		if(n == 5){ // If case to display 5
		val= 0b10110110; 
		//	 XXGXDCBAFE
		}
		if(n == 6){ // If case to display 6
		val= 0b10110111; 
		//	 XXGXDCBAFE
		}
		if(n == 7){ // If case to display 7
		val= 0b00011100; 
		//	 XXGXDCBAFE
		}
		if(n == 8){ // If case to display 8
		val= 0b10111111; 
		//	 XXGXDCBAFE
		}
		if(n == 9){ // If case to display 9
		val= 0b10111110; 
		//	 XXGXDCBAFE
		}
	return val; //Returns the value to the main
}
//----------------------------------------------------------------------------

//------------------- This is the main body of the program -------------------
int main(void)
{
//======== These are the variables we will be using for this program ========= 
	int adc_value, temperature; //We are going to get the adc_value, and 
	int firstseg, secondseg, counter; // First and second digit of the seven seg
	//led, and the counter


//======== Ports, inputs and outputs we need to set up for program ===========
	DDRB = 0b11111111;	//It selects which segments to light up in the current 7 seg 
	DDRD = 0b00111110;	//Selects which digit to light up in the bar and also TX/RX
	PORTD = 0b00111110; //D5-D2 acts as the selector. D1 acts as transmitter. D0 acts as reciever

	DDRC = 0;           //This enables ACD0 for analog input because of temp
						//sensor
	ADCSRA = 0x87;      //This enables the prescaler of 128 to ADC
	ADMUX = 0xC0;       //1.1 V reference and ADC0 left justified


//= This is the while loop that gets the value from sensor and displays to 7seg =
	while(1)
	{
		ADCSRA|=(1<<ADSC);					//Command to start the conversion

		while ((ADCSRA&(1<<ADIF))==0);      //Waiting for converion to finish
		adc_value = ADCL + (ADCH * 256);    //We get both numbers from acd high and low and
											//put them in one

		temperature = ((adc_value * 1.1) / 1024) * 100; //Convert the value into integer
		temperature = (temperature * 9) / 5 + 32;       //Convert Celsius to Fahrenheit

		secondseg = temperature/10;                     //Get each digit separately
		firstseg = temperature - secondseg*10;			//We compute the first seg
		counter = 0;						//Counter to delay the refreshing rate of the
								//LED just a little because it was updating the temperature
								//too fast. 
		while(counter<=250)		// COunter to display the temperature refreshing at a good pace
		{

			PORTD = 0b00101110; // Enable LED2 to display the number
			PORTB = LEDoutput(secondseg); // Selects which Segs to turn on 
			_delay_ms(2); // Keep on for 1 ms

			PORTD = 0b00011110; // Enable LED3 to display the number
			PORTB = LEDoutput(firstseg); // Selects which segs to turn on
			_delay_ms(2); // Keep on for 1 ms

			PORTD = 0b00111110; // Turn Leds off
			counter++; //Update the counter
		}
	}
	return 0;
}




